#!/usr/bin/perl
#this will automatically find out the location of this script
use Cwd 'abs_path';
#line var contains path to this script
$line = abs_path($0);
chomp $line;
@DR_array = split('/',$line);
pop(@DR_array);
pop(@DR_array);
$dir = join("/",@DR_array);

#package will help to read config paramater
use Getopt::Long;
&Getopt::Long::GetOptions(
'config=s'      => \$config
);

#this script will help to read the config file parameters
require "$dir/bin/CONFIG.pl";
getDetails($config);

#all the parameters mentioned in the config file are now copied to local variables
my $output_direc = $config{"OUTPUT_FOLDER"};
my $dirtemp = $config{"TEMP_FOLDER"};
my $file_error = $config{"ERROR_FILE"};
my $file_sampleqc = $config{"QC_FILE"};
my $file_affy = $config{"AFFY6"};
my $file_illreal = $config{"ILLREAL"};
my $file_illallel = $config{"ILLALLELE"};
my $file_annot_ill_allele = $config{"ANNOT_ILL_ALLELE"};
my $file_sdrf = $config{"SDRF"};
my $AT_illreal = $config{"ILLREAL_AT"};
my $AT_illallel = $config{"ILLALLELE_AT"};

print "OUTPUT DIREC: $output_direc\tTEMP DIREC: $dirtemp\tERROR FILE: $file_error\tSAMPLE QC: $file_sampleqc\tAFFY6: $file_affy\tILLREAL: $file_illreal\tILLALLELE: $file_illallel\tSDRF: $file_sdrf \n";
chomp $output_direc;
chomp $dirtemp;
chomp $file_error;
chomp $file_sampleqc;
chomp $file_affy;
chomp $file_illreal;
chomp $file_illallel;
chomp $file_sdrf;
chomp $file_annot_ill_allele;
$dirtemp =~ s/\/$//g;
$output_direc  =~ s/\/$//g;

#checking if parameters are empty
if($file_affy eq "" && $file_affy ne "NA")
{
	die "entered affy directory does not exists\n";
}
if($file_illreal eq "" && $file_illreal ne "NA")
{
        die "entered ill real directory does not exists\n";
}
if($file_illallel eq "" && $file_illallel ne "NA")
{
        die "entered ill allele directory does not exists\n";
}
if($output_direc eq "")
{
        die "entered output directory is empty\n";
}
if($dirtemp eq "")
{
        die "entered temporary directory is empty\n";
}
if($file_error eq "")
{
        die "entered error file is empty\n";
}
if($file_sampleqc eq "")
{
        die "entered sample qc is empty\n";
}
#creating the temp and outputdirectories if not exists
if (!(-d $output_direc )) 
{
	system("mkdir -p $output_direc");
}
if (!(-d $dirtemp ))
{
	system("mkdir -p $dirtemp");
}

#creating the temp directory
$round = sprintf("%.0f", rand()*time());

#$round = 46256540;
$temp_dir = $dirtemp."/$round";

system("mkdir $temp_dir");
system("mkdir -p $output_direc");
system("mkdir $output_direc/NORMAL");
system("mkdir $output_direc/TUMOR");
#checking if the input directory exists
#if (!(-d $input_direc )) {
#	print "There is a no input directory!";
#}



@file_sdrf = split(',',$file_sdrf);
#print "test @file_affy\n";
=head
if($file_affy ne "NA")
{
	#processing start
	@file_affy = split(',',$file_affy);
	#testing if directories exists
	for($i=0;$i<@file_affy;$i++)
	{
		$file_affy[$i] =~ s/\/$//g;
		if (!(-d $file_affy[$i] ))
		{
				die "directory not exists $file_affy[$i]\n";
		}
	}
}
if($file_illreal ne "NA")
{
	@file_illreal = split(',',$file_illreal);
	for($i=0;$i<@file_illreal;$i++)
	{
		$file_illreal[$i] =~ s/\/$//g;
			if (!(-d $file_illreal[$i] ))
			{
					die "directory not exists $file_illreal[$i]\n";
			}
	}
}
=cut
if($file_illallel ne "NA")
{
	@file_illallel = split(',',$file_illallel);
	for($i=0;$i<@file_illallel;$i++)
	{
		$file_illallel[$i] =~ s/\/$//g;
			if (!(-d $file_illallel[$i] ))
			{
					die "directory not exists $file_illallel[$i]\n";
			}
	}
}

#collecting all array type files in the array
@all_files = ();
@all_files_qc = ();
@all_platform = ();
#print "test $dir $temp_dir\n";
#processing affy directory
=head
$affy_dir = "AFFY_$round";
system("mkdir $temp_dir/$affy_dir");
#processing the affy data
if($file_affy ne 'NA')
{
	open(WRQC,">$temp_dir/$affy_dir/combined_QC") or die "not able to write $temp_dir/$affy_dir/combined_QC\n";
	push(@all_files_qc,"$temp_dir/$affy_dir/combined_QC");
	unshift(@all_platform,"AFFY_6");
	#processing each affy directory if you have multiple
	for($i=0;$i<@file_affy;$i++)
	{
		system("mkdir $temp_dir/$affy_dir/dir_$i");
		open(BUFF,"ls $file_affy[$i] |");
		$numsamples = 5;
		$num_incr = 0;
		$num_affy_dir =1;
		#readin all the names of the birdseed files and copying to sub directories
		system("mkdir $temp_dir/$affy_dir/dir_$i/$num_affy_dir");
		while(<BUFF>)
		{
        		#print $_;
        		chomp($_);
        		$num_incr++;
        		if($num_incr == $numsamples)
        		{
                		$num_affy_dir++;
                		system("mkdir $temp_dir/$affy_dir/dir_$i/$num_affy_dir");
                		$num_incr=0;
        		}
        		system("cp $file_affy[$i]/$_ $temp_dir/$affy_dir/dir_$i/$num_affy_dir");
		}
		#creating tped file for subdirectories
		for($j=1;$j<=$num_affy_dir;$j++)
		{
        		$sys ="perl $dir/bin/perl_create_affy_tcga.pl -d $temp_dir/$affy_dir/dir_$i/$j -a $dir/bin/GenomeWideSNP_6.na30.annot.csv -t $temp_dir/$affy_dir/dir_$i/$j -s $file_sdrf[$i]";
        		print $sys."\n";
        		system($sys);
        		system("$dir/bin/plink --tfile $temp_dir/$affy_dir/dir_$i/$j --make-bed --out $temp_dir/$affy_dir/dir_$i/$j");
		}
		#creating file for merging
		open(BUFF,">$temp_dir/$affy_dir/dir_$i/file.txt") or die "no file exits\n";
		for($j=2;$j<=$num_affy_dir;$j++)
		{
        		print BUFF "$temp_dir/$affy_dir/dir_$i/$j.bed $temp_dir/$affy_dir/dir_$i/$j.bim $temp_dir/$affy_dir/dir_$i/$j.fam\n";
		}
		system("$dir/bin/plink --bfile $temp_dir/$affy_dir/dir_$i/1 --merge-list $temp_dir/$affy_dir/dir_$i/file.txt --recode --transpose --out $temp_dir/$affy_dir/dir_$i/combined");
		#system("mv $temp_dir/$affy_dir/dir_$i/combined.tped .tped");
		#system("mv $temp_dir/$affy_dir/dir_$i/combined.tfam $maindir/AFFY/$tissue.tfam");
		#system("rm -rf $temp/*");
	}
	#generating the qc files
	if(@file_affy == 1)
	{

		system("mv $temp_dir/$affy_dir/dir_0/combined.tped $temp_dir/$affy_dir/affy.tped");
		system("mv $temp_dir/$affy_dir/dir_0/combined.tfam $temp_dir/$affy_dir/affy.tfam");
		system("/usr/bin/perl /data4/bsi/RandD/Workflow/genotyping_QC_FWD/perl_only_FWD_QC_multiplearray.pl -d /data4/bsi/RandD/Workflow/temp -t $temp_dir/$affy_dir/affy.tped -a AFFY_6 -f $temp_dir/$affy_dir/affy.tfam  -o $temp_dir/$affy_dir");	
		open(QC,"$temp_dir/$affy_dir/sampleqc_affy.tped") or die "no sampleqc $temp_dir/$affy_dir/sampleqc_affy.tped file found\n";
		#system("rm -rf $temp_dir/$affy_dir/dir_0/*");
		$line =<QC>;
		while(<QC>)
		{
			chomp($_);
			print WRQC $_."\n";
		}
	}	
	else
	{
		die "need to edit the code tohandle multiple affy file\n";
	}	
	push(@all_files,"$temp_dir/$affy_dir/fwdStrandResults_affy");
}

#PROCESSING ILLUMINA REAL DATA
if($file_illreal ne 'NA')
{
	$ill_real_dir = "ILLREAL_$round";
	system("mkdir $temp_dir/$ill_real_dir");
	open(WRQC,">$temp_dir/$ill_real_dir/combined_QC") or die "not able to write $temp_dir/$ill_real_dir/combined_QC\n";
	push(@all_files_qc,"$temp_dir/$ill_real_dir/combined_QC");
	unshift(@all_platform,$AT_illreal);
	#processing the illumina real data in multiple folders
	for($i=0;$i<@file_illreal;$i++)
	{
		$sys ="perl $dir/bin/perl_create_illu_tcga_real.pl -t $temp_dir/$ill_real_dir/ill_real$i -d $file_illreal[$i]";
        print $sys."\n";
        system($sys);
		system("/usr/bin/perl /data4/bsi/RandD/Workflow/genotyping_QC_FWD/perl_only_FWD_QC_multiplearray.pl -d /data4/bsi/RandD/Workflow/temp -t $temp_dir/$ill_real_dir/ill_real$i.tped -a $AT_illreal -f $temp_dir/$ill_real_dir/ill_real$i.tfam  -o $temp_dir/$ill_real_dir");
		system("$dir/bin/plink --tfile $temp_dir/$ill_real_dir/fwdStrandResults_ill_real$i --make-bed --out $temp_dir/$ill_real_dir/fwdStrandResults_ill_real$i");
	}
	#copying the first qc file
	open(QC,"$temp_dir/$ill_real_dir/sampleqc_ill_real0.tped") or die "no sampleqc $temp_dir/$ill_real_dir/sampleqc_ill_real0.tped file found\n";
	$line =<QC>;
	while(<QC>)
	{
		chomp($_);
		@array = split(/\t/,$_);
		if(!exists($qc_illreal{$array[0]}))
		{
			$qc_illreal{$array[0]} =1;
			print WRQC $_."\n";
		}
	}
	#if you have directory or multiple directories
	if(@file_illreal ==1)
    {
                system("mv $temp_dir/$ill_real_dir/fwdStrandResults_ill_real0.tped $temp_dir/$ill_real_dir/FINAL_ILLREAL.tped");
                system("mv $temp_dir/$ill_real_dir/fwdStrandResults_ill_real0.tfam $temp_dir/$ill_real_dir/FINAL_ILLREAL.tfam");
				
    }
	#if you multiple directoried merging all the files to one
	else
	{
		open(BUFF,">$temp_dir/$ill_real_dir/file.txt") or die "no file exits\n";
        for($i=1;$i<=@file_illreal;$i++)
        {
            print BUFF "$temp_dir/$ill_real_dir/fwdStrandResults_ill_real$i.bed $temp_dir/$ill_real_dir/fwdStrandResults_ill_real$i.bim $temp_dir/$ill_real_dir/fwdStrandResults_ill_real$i.fam\n";
			open(QC,"$temp_dir/$ill_real_dir/sampleqc_ill_real$i.tped") or die "no sampleqc $temp_dir/$ill_real_dir/sampleqc_ill_real$i.tped file found\n";
			$line =<QC>;
			while(<QC>)
			{
				chomp($_);
				@array = split(/\t/,$_);
				if(!exists($qc_illreal{$array[0]}))
				{
					$qc_illreal{$array[0]} =1;
					print WRQC $_."\n";
				}	
			}
		}
        system("$dir/bin/plink --bfile $temp_dir/$ill_real_dir/fwdStrandResults_ill_real0 --merge-list $temp_dir/$ill_real_dir/file.txt --recode --transpose --out $temp_dir/$ill_real_dir/FINAL_ILLREAL");

	}
	push(@all_files,"$temp_dir/$ill_real_dir/FINAL_ILLREAL");

}
=cut
#PROCESSING ILLUMINA ALLELE DATA
if($file_illallel ne 'NA')
{
	$ill_allele_dir = "ILLALLELE_$round";
	system("mkdir $temp_dir/$ill_allele_dir");
	open(WRQC,">$temp_dir/$ill_allele_dir/combined_QC") or die "not able to write $temp_dir/$ill_allele_dir/combined_QC\n";
	push(@all_files_qc,"$temp_dir/$ill_allele_dir/combined_QC");
	unshift(@all_platform,$AT_illallel);
	for($i=0;$i<@file_illallel;$i++)
	{
		$sys ="perl $dir/bin/perl_create_illu_tcga.pl -a $dir/bin/$file_annot_ill_allele -t $temp_dir/$ill_allele_dir/ill_allele$i -d $file_illallel[$i]";
        	print $sys."\n";
        system($sys);
		system("/usr/bin/perl /data4/bsi/RandD/Workflow/genotyping_QC_FWD/perl_only_FWD_QC_multiplearray.pl -d /data4/bsi/RandD/Workflow/temp -t $temp_dir/$ill_allele_dir/ill_allele$i.tped -a $AT_illallel -f $temp_dir/$ill_allele_dir/ill_allele$i.tfam  -o $temp_dir/$ill_allele_dir");
		system("$dir/bin/plink --tfile $temp_dir/$ill_allele_dir/fwdStrandResults_ill_allele$i --make-bed --out $temp_dir/$ill_allele_dir/fwdStrandResults_ill_allele$i");
	}
	#copying the first qc file
	open(QC,"$temp_dir/$ill_allele_dir/sampleqc_ill_allele0.tped") or die "no sampleqc $temp_dir/$ill_allele_dir/sampleqc_ill_allele0.tped file found\n";
	$line =<QC>;
	while(<QC>)
	{
		chomp($_);
		@array = split(/\t/,$_);
		if(!exists($qc_illallele{$array[0]}))
		{
			$qc_illallele{$array[0]} =1;
			print WRQC $_."\n";
		}
	}
	#if you have one or multiple illumina allele directories then merging all of them to one
	if(@file_illallel ==1)
	{
		system("mv $temp_dir/$ill_allele_dir/fwdStrandResults_ill_allele0.tped $temp_dir/$ill_allele_dir/FINAL_ILLALLELE.tped");
		system("mv $temp_dir/$ill_allele_dir/fwdStrandResults_ill_allele0.tfam $temp_dir/$ill_allele_dir/FINAL_ILLALLELE.tfam");
	}
	else
    {
                open(BUFF,">$temp_dir/$ill_allele_dir/file.txt") or die "no file exits\n";
                for($i=1;$i<@file_illallel;$i++)
                {
                        print BUFF "$temp_dir/$ill_allele_dir/fwdStrandResults_ill_allele$i.bed $temp_dir/$ill_allele_dir/fwdStrandResults_ill_allele$i.bim $temp_dir/$ill_allele_dir/fwdStrandResults_ill_allele$i.fam\n";
						open(QC,"$temp_dir/$ill_allele_dir/sampleqc_ill_allele$i.tped") or die "no sampleqc $temp_dir/$ill_allele_dir/sampleqc_ill_allele$i.tped file found\n";
						$line =<QC>;
						while(<QC>)
						{
							chomp($_);
							@array = split(/\t/,$_);
							if(!exists($qc_illallele{$array[0]}))
							{
								$qc_illallele{$array[0]} =1;
								print WRQC $_."\n";
							}
						}
				}
                system("$dir/bin/plink --bfile $temp_dir/$ill_allele_dir/fwdStrandResults_ill_allele0 --merge-list $temp_dir/$ill_allele_dir/file.txt --recode --transpose --out $temp_dir/$ill_allele_dir/FINAL_ILLALLELE");

    }
	push(@all_files,"$temp_dir/$ill_allele_dir/FINAL_ILLALLELE");
}
print "merging all files started\n";
=head
#merging all the different array type files
if(@all_files > 1)
{
	$file_merge = "";
	for($i=0;$i<@all_files;$i++)
	{
		$file_merge ="$file_merge $all_files[$i]"; 
	}
	$file_qc_merge = "";
	for($i=0;$i<@all_files_qc;$i++)
	{
		$file_qc_merge = "$file_qc_merge $all_files_qc[$i]";
	}
	$file_all_platform = "";
	for($i=0;$i<@all_platform;$i++)
	{
		if(!exists($platform{$all_platform[$i]}))
		{
			$platform{$all_platform[$i]} =1;
			$file_all_platform = "$file_all_platform,$all_platform[$i]";
		}
	}	
	$file_all_platform =~ s/^,//g;
	system("cat $file_qc_merge >$temp_dir/all_merged_qc");
	#merging tped file manually
	print "perl $dir/bin/perl_simplemergetpedfile.pl $file_merge $temp_dir/all_merged\n";
	system("perl $dir/bin/perl_simplemergetpedfile.pl $file_merge $temp_dir/all_merged");
	#filtring duplicate sample and aliquots
	print "perl $dir/bin/perl_extract_tfamsample_qc.pl $temp_dir/all_merged.tfam $temp_dir/all_merged_qc $temp_dir/all_merged_qc_new\n";
	system("perl $dir/bin/perl_extract_tfamsample_qc.pl $temp_dir/all_merged.tfam $temp_dir/all_merged_qc $temp_dir/all_merged_qc_new");
	
	print "perl $dir/bin/perl_remove_duplicates_plinkfile.pl -t $temp_dir/all_merged -q $temp_dir/all_merged_qc_new\n";
	system("perl $dir/bin/perl_remove_duplicates_plinkfile.pl -t $temp_dir/all_merged -q $temp_dir/all_merged_qc_new");
	open(FIL,"$temp_dir/all_merged_removeddup.tped") or die "no file exists\n";
	open(WRFIL,">$temp_dir/all_merged_removeddup1.tped") or die "not able to write\n";
	while(<FIL>)
	{
		chomp($_);
		if($_ !~ m/^0 /)
		{
			print WRFIL $_."\n";
		}
	}
	close(FIL);
	close(WRFIL);

	print "mv $temp_dir/all_merged_removeddup1.tped $temp_dir/all_merged_removeddup.tped\n";
	system("mv $temp_dir/all_merged_removeddup1.tped $temp_dir/all_merged_removeddup.tped");
	#running thw forward strand & qc program
	print "/usr/bin/perl /data4/bsi/RandD/Workflow/genotyping_QC_FWD/perl_only_FWD_QC_multiplearray.pl -d /data4/bsi/RandD/Workflow/temp -t $temp_dir/all_merged_removeddup.tped -a $file_all_platform -f $temp_dir/all_merged_removeddup.tfam  -o $temp_dir\n";
	system("/usr/bin/perl /data4/bsi/RandD/Workflow/genotyping_QC_FWD/perl_only_FWD_QC_multiplearray.pl -d /data4/bsi/RandD/Workflow/temp -t $temp_dir/all_merged_removeddup.tped -a $file_all_platform -f $temp_dir/all_merged_removeddup.tfam  -o $temp_dir");
	#separating to normal & tumor
	print "/usr/bin/perl $dir/bin/perl_separate_tumor_normal.pl $temp_dir fwdStrandResults_all_merged_removeddup nobin\n";
	system("/usr/bin/perl $dir/bin/perl_separate_tumor_normal.pl $temp_dir fwdStrandResults_all_merged_removeddup nobin");
	system("perl $dir/bin/perl_extract_sampleqc_tumor_normal.pl $temp_dir/fwdStrandResults_all_merged_removeddup_normal.tfam $temp_dir/sampleqc_all_merged_removeddup.tped $temp_dir/normal_all_merged_qc");
	system("perl $dir/bin/perl_extract_sampleqc_tumor_normal.pl $temp_dir/fwdStrandResults_all_merged_removeddup_tumor.tfam $temp_dir/sampleqc_all_merged_removeddup.tped $temp_dir/tumor_all_merged_qc");
	
	#moving the data to the output normal
	system("mv $temp_dir/fwdStrandResults_all_merged_removeddup_normal.tfam $output_direc/NORMAL");
	system("mv $temp_dir/fwdStrandResults_all_merged_removeddup_normal.tped $output_direc/NORMAL");
	system("mv $temp_dir/normal_all_merged_qc $output_direc/NORMAL");
	system("cp $temp_dir/fwdStrandFlippedInd_all_merged_removeddup.txt $output_direc/NORMAL");
	#moving the data to the output tumor
	system("mv $temp_dir/fwdStrandResults_all_merged_removeddup_tumor.tfam $output_direc/TUMOR");
	system("mv $temp_dir/fwdStrandResults_all_merged_removeddup_tumor.tped $output_direc/TUMOR");
	system("mv $temp_dir/tumor_all_merged_qc $output_direc/TUMOR");
	system("cp $temp_dir/fwdStrandFlippedInd_all_merged_removeddup.txt $output_direc/NORMAL");
}
elsif(@all_files == 1 && $file_affy ne 'NA')
{
	push(@all_files,"/data4/bsi/RandD/Workflow/temp/1289962810/AFFY_1289962810/fwdStrandResults_affy");
	@tt = split(/\//,$all_files[0]);
	$ff=pop(@tt);
	$all_files[0] = join('/',@tt);
	undef(@tt);
	$temp_dir = $all_files[0];
	$file = $ff;
	#print "$all_files[0] $ff\n";
	#separating to normal & tumor
	system("mv $temp_dir/$file.tfam $temp_dir/fwdStrandResults_all_merged_removeddup.tfam");
	system("mv $temp_dir/$file.tped $temp_dir/fwdStrandResults_all_merged_removeddup.tped");
	system("mv  $temp_dir/fwdStrandFlippedInd_affy.txt $temp_dir/fwdStrandFlippedInd_all_merged_removeddup.txt");
	system("mv 	$temp_dir/combined_QC $temp_dir/sampleqc_all_merged_removeddup.tped");	
	print "/usr/bin/perl $dir/bin/perl_separate_tumor_normal.pl $temp_dir fwdStrandResults_all_merged_removeddup nobin\n";
	system("/usr/bin/perl $dir/bin/perl_separate_tumor_normal.pl $temp_dir fwdStrandResults_all_merged_removeddup nobin");
	system("perl $dir/bin/perl_extract_sampleqc_tumor_normal.pl $temp_dir/fwdStrandResults_all_merged_removeddup_normal.tfam $temp_dir/sampleqc_all_merged_removeddup.tped $temp_dir/normal_all_merged_qc");
	system("perl $dir/bin/perl_extract_sampleqc_tumor_normal.pl $temp_dir/fwdStrandResults_all_merged_removeddup_tumor.tfam $temp_dir/sampleqc_all_merged_removeddup.tped $temp_dir/tumor_all_merged_qc");
	
	#moving the data to the output normal
	system("mv $temp_dir/fwdStrandResults_all_merged_removeddup_normal.tfam $output_direc/NORMAL");
	system("mv $temp_dir/fwdStrandResults_all_merged_removeddup_normal.tped $output_direc/NORMAL");
	system("mv $temp_dir/normal_all_merged_qc $output_direc/NORMAL");
	system("cp $temp_dir/fwdStrandFlippedInd_all_merged_removeddup.txt $output_direc/NORMAL");
	system("cp $temp_dir/snps_failed_QC $output_direc/NORMAL");
	#moving the data to the output tumor
	system("mv $temp_dir/fwdStrandResults_all_merged_removeddup_tumor.tfam $output_direc/TUMOR");
	system("mv $temp_dir/fwdStrandResults_all_merged_removeddup_tumor.tped $output_direc/TUMOR");
	system("mv $temp_dir/tumor_all_merged_qc $output_direc/TUMOR");
	system("cp $temp_dir/fwdStrandFlippedInd_all_merged_removeddup.txt $output_direc/TUMOR");
	system("cp $temp_dir/snps_failed_QC $output_direc/TUMOR");
	
}
else
{
	die "all files array is empty\n";
}
